<?php
//error_reporting(E_ALL);
//ini_set('display_errors', 1);

require_once 'MainPageWidget.php';
class NodeStatWidget extends MainPageWidget {
	public $title = 'Статистика узла';
  public $from = 'node';
  public $community = array();
  public $community_list = array();
  public $tdi = array();
  public $tdi_list = array();
  public $widget_view = 'WG_NodeStatWidget';
  
	public function Render() {
    $this->ctx->rh->UseClass('RL_CyrDate');
		$this->ctx->rh->UseClass("RL_URLGenerator");
    
    $this->userscount+=0;
    if ( empty($this->userscount) ) {
      $this->userscount = $this->users_count;
    }
    
		$content['title'] = $this->getParam("title");
    $content['from'] = $this->getParam("from");
    
    //print '<pre style="text-align:left;">'; print_r($this);exit;
    
    $communityWhere = array();
    foreach ( $this->community_list as $community_oid ) {
      $tdi = $this->getTdiList( $community_oid );
      
      if ( !empty($tdi) ) {
        $tdi_list = array();
        
        foreach ( $this->tdi_list as $tdi_oid ) {
          foreach ($tdi as $val) {
            if ( $tdi_oid == $val['oid'] || 0 == $tdi_oid ) {
              $tdi_list[] = $tdi_oid;
              break;
            }
          }
        }
        
        $communityWhere[] = "(a.community_oid = {$community_oid} and a.tdi_oid in (" . join(',', $tdi_list) . '))';
      } else {
        $communityWhere[] = "a.community_oid = {$community_oid}";
      }
    }
      
    $community_string = '(' . join(' or ', $communityWhere) . ')';
    
    //print '<pre style="text-align:left;">'; print_r($community_string);exit;
    
    $content['item_count'] = 0;
    $content['comments_count'] = 0;
    $content['user_count'] = 0;
    
    if ( 'node' == $this->from ) {
      $sql = 'select count(*) as item_count ' .
      " from {$this->ctx->store->db->db_prefix}class_allfeed a " .
      " left join {$this->ctx->store->db->db_prefix}objects o on o.oid = a.oid " .
      ' where o.is_deleted = 0 and a.community_oid > 0';
    } else {
      $sql = 'select count(*) as item_count ' .
      " from {$this->ctx->store->db->db_prefix}class_allfeed a " .
      " left join {$this->ctx->store->db->db_prefix}objects o on o.oid = a.oid " .
      ' where o.is_deleted = 0 and a.community_oid > 0 and ' . $community_string;
    }
      
    $result = $this->ctx->store->query($sql);
      
    if ( $result instanceof MysqlDBResult ) {
      $row = $result->next();
      
      if ( isset($row['item_count']) ) {
        $content['item_count'] = $row['item_count'];
      }
    }
      
    if ( 'node' == $this->from ) {
      $sql = 'select count(*) as comments_count ' .
      " from {$this->ctx->store->db->db_prefix}class_comment c " .
      " left join {$this->ctx->store->db->db_prefix}class_allfeed a on a.oid = c.item_oid " .
      " left join {$this->ctx->store->db->db_prefix}objects o on o.oid = a.oid " .
      " left join {$this->ctx->store->db->db_prefix}objects o2 on o2.oid = c.oid " .
      ' where o.is_deleted = 0 and o2.is_deleted = 0 and a.community_oid > 0';
    } else {
      $sql = 'select count(*) as comments_count ' .
      " from {$this->ctx->store->db->db_prefix}class_comment c " .
      " left join {$this->ctx->store->db->db_prefix}class_allfeed a on a.oid = c.item_oid " .
      " left join {$this->ctx->store->db->db_prefix}objects o on o.oid = a.oid " .
      " left join {$this->ctx->store->db->db_prefix}objects o2 on o2.oid = c.oid " .
      ' where o.is_deleted = 0 and o2.is_deleted = 0 and a.community_oid > 0 and ' . $community_string;
    }
    
    $result = $this->ctx->store->query($sql);
      
    if ( $result instanceof MysqlDBResult ) {
      $row = $result->next();
      
      if ( isset($row['comments_count']) ) {
        $content['comments_count'] = $row['comments_count'];
      }
    }
      
    if ( 'node' == $this->from ) {
      //$sql = 'select count(*) as user_count from (select m.user_oid ' .
      //" from {$this->ctx->store->db->db_prefix}class_member m " .
      //" left join {$this->ctx->store->db->db_prefix}objects o on o.oid = m.oid " .
      //' where o.is_deleted = 0 group by m.user_oid) users';
      
      $sql = "select count(*) as user_count from {$this->ctx->store->db->db_prefix}class_user u " .
      " left join {$this->ctx->store->db->db_prefix}objects o on o.oid = u.oid " .
      ' where o.is_deleted = 0';
      
      //print $sql;exit;
    } else {
      $usersWhere = array();
      foreach ( $this->community_list as $community_oid ) {
         $usersWhere[] = "m.community_oid = {$community_oid}";
      }
        
      $users_string = '(' . join(' or ', $usersWhere) . ')';
      
      $sql = 'select count(*) as user_count from (select m.user_oid ' .
      " from {$this->ctx->store->db->db_prefix}class_member m " .
      " left join {$this->ctx->store->db->db_prefix}objects o on o.oid = m.oid " .
      " where o.is_deleted = 0 and {$users_string} group by m.user_oid) users";
    }
    
    $result = $this->ctx->store->query($sql);
      
    if ( $result instanceof MysqlDBResult ) {
      $row = $result->next();
      
      if ( isset($row['user_count']) ) {
        $content['user_count'] = $row['user_count'];
      }
    }
    
    $this->content = $content;
    
		return $this;
  }
  
  public function settingsForm() {
    if ( empty($this->community) ) {
      $rs = $this->ctx->store->find("objects where objects.type = :0 and objects.is_deleted=0 order by objects.ctime desc", array("fcommunity"));
  		while ($obj = $rs->next()) {
  			$this->community[] = array_merge($obj->data, array(
  				'oid' => $obj->id
  			));
  		}
    }
    
    foreach ( $this->community as $key=>$val ) {
      foreach ( $this->community_list as $oid ) {
        if ( $oid == $val['oid'] ) {
          $this->community[$key]['checked'] = true;
          break;
        }
      }
      $tdi_list = $this->getTdiList( $val['oid'] );
      
      foreach ($tdi_list as $index=>$tdi) {
        foreach ($this->tdi_list as $present) {
          if ( $present == $tdi['oid'] ) {
            $tdi_list[$index]['checked'] = true;
          } else if ( 0 == $present && isset($this->community[$key]['checked'])
          && $this->community[$key]['checked']) {
            $this->community[$key]['root_checked'] = true;
          }
        }
      }
      
      $this->community[$key]['tdi'] = $tdi_list;
    }
    
		if ( !isset($this->params) || !isset($this->params['title']) ) {
			$this->params['title'] = $this->title;
      $this->params['from'] = $this->from;
      $this->params['community'] = $this->community;
		}
    
		//return $this->params;
	}
  
  public function setParams( $params ) {
    foreach ( $params as $key=>$val ) {
      if ( isset($this->$key) ) {
        $this->$key = $val;
      }
    }
  }
}